package com.goods.business.service.impl;

import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ProductCategoryMapper;
import com.goods.business.service.ProductCategoryService;
import com.goods.common.error.SystemCodeEnum;
import com.goods.common.error.SystemException;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * description:
 * create by sxy 2021/6/7
 */
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Autowired
    private ProductCategoryMapper productMapper;
    //物资类别列表显示
    @Override
    public PageVO<ProductCategoryTreeNodeVO>categoryTree(Integer pageNum, Integer pageSize) {

        Example o = new Example(ProductCategory.class);
        List<ProductCategory> productCategories = productMapper.selectByExample(o);

        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = new ArrayList<>();
        //将查出来的物资类别封装到vo中
        for (ProductCategory productCategory : productCategories) {
            ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
            BeanUtils.copyProperties(productCategory,productCategoryTreeNodeVO);

            productCategoryTreeNodeVOS.add(productCategoryTreeNodeVO);
        }
        List<ProductCategoryTreeNodeVO> build = CategoryTreeBuilder.build(productCategoryTreeNodeVOS);

        List<ProductCategoryTreeNodeVO> page = ListPageUtils.page(build, pageSize, pageNum);

        PageInfo<ProductCategoryTreeNodeVO> info = new PageInfo<>(build);
        return new PageVO<>(info.getTotal(), page);


    }

    //添加前查询二级父类id
    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {

        Example o = new Example(ProductCategory.class);
        List<ProductCategory> productCategories = productMapper.selectByExample(o);

        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = new ArrayList<>();
        //将查出来的物资类别封装到vo中
        for (ProductCategory productCategory : productCategories) {
            ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
            BeanUtils.copyProperties(productCategory,productCategoryTreeNodeVO);

            productCategoryTreeNodeVOS.add(productCategoryTreeNodeVO);
        }
        return CategoryTreeBuilder.buildParent(productCategoryTreeNodeVOS);

    }

    //添加分类
    @Override
    public void add(ProductCategoryVO productCategoryVO ) throws SystemException {
        @NotBlank(message = "类目名称不能为空") String name = productCategoryVO.getName();
        @NotBlank(message = "类目备注不能为空") String remark =  productCategoryVO.getRemark();
        @NotNull(message = "排序号不能为空") Integer sort =  productCategoryVO.getSort();

        Example o = new Example(ProductCategory.class);
        //判断分类是否存在该
        o.createCriteria().andEqualTo("name", name);
        int i = productMapper.selectCountByExample(o);
        if (i != 0) {
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"该分类已经存在");
        }
        ProductCategory productCategory = new ProductCategory();

        BeanUtils.copyProperties(productCategoryVO, productCategory);
        productCategory.setCreateTime(new Date());
        productCategory.setModifiedTime(new Date());
        productMapper.insert(productCategory);

    }

    //修改分类之回显数据
    @Override
    public ProductCategoryVO edit(Long id) {
        ProductCategory productCategory = new ProductCategory();
        ProductCategoryVO productCategoryVO = new ProductCategoryVO();
        productCategory.setId(id);
        ProductCategory productCategory1 = productMapper.selectOne(productCategory);
        BeanUtils.copyProperties(productCategory1, productCategoryVO);
        return productCategoryVO;
    }

    //修改分类
    @Override
    public void update(Long id, ProductCategory productCategory) throws SystemException {
        ProductCategory dbProductCategory = productMapper.selectByPrimaryKey(id);
        //判断非空
        if (dbProductCategory == null) {
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"要更新的类别不存在");
        }
        //接收的数据封装到productCategory
//        ProductCategory productCategory = new ProductCategory();
//        BeanUtils.copyProperties(productCategory, productCategory);
        productCategory.setId(id);
        productCategory.setModifiedTime(new Date());
        productMapper.updateByPrimaryKeySelective(productCategory);

    }

    //删除分类
    @Override
    public void delete(Long id) throws SystemException {
        ProductCategory productCategory = productMapper.selectByPrimaryKey(id);
        if (productCategory == null) {
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"要删除的类别不存在");
        }
        productMapper.deleteByPrimaryKey(id);
    }

}
